package com.hzlh.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hzlh.dao.TCompanyBaseDao;
import com.hzlh.dao.TGeographyDao;
import com.hzlh.model.TCompanyBase;
import com.hzlh.model.TCompanyBaseVo;
import com.hzlh.model.TGeography;
import com.hzlh.service.IAIService;
import com.hzlh.utils.HttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class IAIServiceImpl implements IAIService {
    @Value("${server.port}")
    private String port;
    @Resource
    private TCompanyBaseDao tCompanyBaseDao;

    @Resource
    private TGeographyDao tGeographyDao;

    @Override
    public Map<String, List<Map<String,String>>> analysisCompanyFromMap(List<String> companyNames) {
        Map<String, List<Map<String,String>>> resultMap = new HashMap<>();
        // 企业状态
        List<Map<String, String>> innerList = tCompanyBaseDao.enterpriseStateCount(companyNames);
        resultMap.put("status", innerList);

        // 企业类型
        List<Map<String, String>> typeMapList = tCompanyBaseDao.businessTypeCount(companyNames);
        resultMap.put("type", typeMapList);

        // 行业统计top5
        List<Map<String, String>> industryMapList = tCompanyBaseDao.industryCount(companyNames);
        resultMap.put("industryRank", industryMapList);

        // 年份统计top5
        List<Map<String, String>> yearMapList = tCompanyBaseDao.registrationDateCount(companyNames);
        resultMap.put("yearRank", yearMapList);
        return resultMap;
    }

    @Override
    public void transformCompanyLocation() {
        List<TCompanyBaseVo> list = tCompanyBaseDao.selectList(null);
//        List<TCompanyBase> list = new ArrayList<>(100);
        TCompanyBaseVo mode = null;
        if (!CollectionUtils.isEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                mode = list.get(i);
                if (StringUtils.isNotEmpty(mode.getAddress())) {
                    //String json = HttpUtil.get("http://127.0.0.1:"+port+"/api/syn", null);
                    String url = "http://api.map.baidu.com/geocoding/v3/?address=" + mode.getAddress() + "&output=json&ak=0kyBrwe87UQBKz4NbuhMm2duhcqRmxLj";
                    String json = HttpUtil.get(url, null);
                    JSONObject value = JSON.parseObject(json);
                    JSONObject resultValue = (JSONObject) value.get("result");
                    if (null != resultValue) {
                        JSONObject locationValue = (JSONObject) resultValue.get("location");
                        BigDecimal lng = (BigDecimal) locationValue.get("lng");
                        BigDecimal lat = (BigDecimal) locationValue.get("lat");

                        System.out.println("-----lng:" + lng);
                        System.out.println("-----lat:" + lat);
                        mode.setLocLat(lat);
                        mode.setLocLon(lng);
                        tCompanyBaseDao.update(mode);
                    }
                }
            }
        }
    }

    @Override
    public void transform() {
        List<TGeography> tGeographies = tGeographyDao.selectList(new TGeography());
        List<String> collect = tGeographies.stream().map(TGeography::getName).collect(Collectors.toList());
        collect.stream().forEach(v -> {
            String url = "http://api.map.baidu.com/geocoding/v3/?address=" + v + "&output=json&ak=0kyBrwe87UQBKz4NbuhMm2duhcqRmxLj";
            String json = HttpUtil.get(url, null);
            JSONObject value = JSON.parseObject(json);
            JSONObject resultValue = (JSONObject) value.get("result");
            if (null != resultValue) {
                JSONObject locationValue = (JSONObject) resultValue.get("location");
                BigDecimal lng = (BigDecimal) locationValue.get("lng");
                BigDecimal lat = (BigDecimal) locationValue.get("lat");
                TGeography tGeography = new TGeography();
                tGeography.setName(v);
                tGeography.setLocLat(lat);
                tGeography.setLocLon(lng);
                int i = tGeographyDao.updateArea(tGeography);
                System.out.println("-----name:" + v);
                System.out.println("-----lng:" + lng);
                System.out.println("-----lat:" + lat);
            }
        });
    }
}
